import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/heade',
    name: 'heade',
    component: () => import('../views/heade'),
    children: [
      {
        path: '/users',
        name: 'users',
        component: () => import("../views/userlist.vue")
      },
      {
        path: "/roles",
        name: "roles",
        component: () => import("../views/roleslist.vue")
      },
      {
        path: "/rights",
        name: 'rights',
        component: () => import("../views/rightslist.vue")
      },
      {
        path: "/goods",
        name: "goods",
        component: () => import("../views/goodslist.vue")
      },
      {
        path: "/params",
        name: "params",
        component: () => import("../views/paramslist.vue")
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import("../views/categorieslist.vue")
      },
      {
        path: "/orders",
        name: 'orders',
        component: () => import("../views/orderslist.vue")
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import("../views/reportslist.vue")
      },
      {
        path: '/add',
        name: 'add',
        component: () => import("../views/add.vue")
      },

    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home.vue')
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  var loginstatus = localStorage.getItem('token')
  if (loginstatus) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
